package com.app.webservice;

import java.util.ArrayList;
import java.util.List;

import com.app.entidades.Videos;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class VideoRest {

	
	public List<Videos> getListaVideos() throws Exception{
		String url ="http://lucianokikao.com.br/mobile/video.php?user=q1w2e3r4t5&pass=t5r4e3w2q1";
		String[] resposta = new WebServiceApp().get(url);
		
		if (resposta[0].equals("200")) {
	         Gson gson = new Gson();
	         List<Videos> listaVideos = new ArrayList<Videos>();
	         JsonParser parser = new JsonParser();
	         
	         try{
	        	 JsonElement element = parser.parse(resposta[1]);
	        	 JsonArray array = element.getAsJsonArray();
	        	 for (int i = 0; i < array.size(); i++) {
		             listaVideos.add(gson.fromJson(array.get(i), Videos.class));
		         }
	         }catch(Exception e){
	         	 throw new Exception("Falha ao acessar Internet");
	         }
	         
	         return listaVideos;
	     } else {
	         throw new Exception(resposta[1]);
	     }
	}
}
